/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.organization.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.system.ManagementAccount;
import com.qinyeit.serviceapp.entity.system.ManagementRole;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.service.system.ManagementAccountService;
import com.qinyeit.serviceapp.service.system.ManagementRoleService;
import com.qinyetech.springstage.core.entity.search.Searchable;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.lang.StringUtils;
import com.qinyetech.springstage.core.repository.jdbc.PageNamedParameterJdbcTemplate;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.Employee;
import com.qinyeit.serviceapp.repository.organization.EmployeeRepository;
import com.qinyeit.serviceapp.service.organization.EmployeeService;

import java.util.List;
import java.util.Map;
import java.util.Set;

/** 
 * ClassName: EmployeeServiceImpl <br/> 
 * Function: 员工 业务实现 <br/> 
 * date: 2018-11-20 13:17:56 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class EmployeeServiceImpl extends BaseService<Employee, Long> implements EmployeeService {


	@Autowired
	private EmployeeRepository employeeRepository;
	@Autowired
	private PageNamedParameterJdbcTemplate jdbcTemplate;
	@Autowired
	private ManagementAccountService managementAccountService;


	@Override
	public Page<Map<String, Object>> findallPage(Searchable searchable, Long merchantGroupId) {
		String sql = "SELECT e.*,a.account as account_name,a.locked as account_locked,\n" +
				"d.name as district_name,d.parent_id as district_parent_id, \n" +
				"d.parent_ids as district_parent_ids,\n" +
				"m.name as department_name\n" +
				"from employees e\n" +
				"LEFT JOIN management_accounts a on a.id = e.account_id\n" +
				"LEFT JOIN districts d on d.id = e.district_id\n" +
				"LEFT JOIN departments m on m.id = e.department_id\n" +
				"where e.is_deleted = 0 and  (e.account_id is null or a.admin = 0)\n" +
				"and e.merchant_group_id = :merchantGroupId";

		QyMap map = QyMap.map("merchantGroupId",merchantGroupId);

		String name = searchable.getValue("name_like");
		if(StringUtils.isNotBlank(name)){
			sql += " and e.name like :name";
			map.setv("name","%"+name+"%");
		}
		String state = searchable.getValue("state_eq");
		if (StringUtils.isNotBlank(state)){
			sql += " and a.locked=:locked";
			if(state.equals("NORMAL")){
				map.setv("locked",0); //未锁定
			}else {
				map.setv("locked",1); //锁定
			}
		}

		String districtId = searchable.getValue("districtId_eq");
		if(StringUtils.isNotBlank(districtId)){
			sql += " and (d.id = :districtId or d.parent_ids like :districtParentIds)";
			map.setv("districtId",districtId);
			map.setv("districtParentIds","%/"+districtId+"/%");
		}
		Page<Map<String,Object>> list = jdbcTemplate.queryForList(sql,map,searchable.getPage());

		QyMap qyMap = null;
		//所属岗位
		if (CollectionUtils.isNotEmpty(list.getContent())){
			for (Map<String, Object> item : list) {
				if(item.get("account_id")!=null){
					qyMap = getRoles(Long.valueOf(item.get("account_id").toString()));
					item.put("roles",qyMap.getString("rolestr"));
				}
			}
		}

		return list;
	}



	public QyMap getRoles(Long id){
		QyMap map = QyMap.NEW();
		if(id==null){
			map.setv("rolestr","");
			return map;
		}

		ManagementAccount account = managementAccountService.findById(id);
		if(account==null){
			map.setv("rolestr","");
			return map;
		}
		String rolestr = "";
		Set<ManagementRole> roles = account.getRoles();
		List<Long> ids = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(roles)){
			for (ManagementRole role : roles) {
				rolestr += role.getName()+" ";
				ids.add(role.getId());
			}
		}
		map.setv("rolestr",rolestr);
		map.setv("ids",ids);

		return map;
	}
}